<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditUserList">
      <el-table-column label="账号" align="center" prop="username"/>
      <el-table-column label="学号/工号" align="center" prop="number"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 0">男</span>
          <span v-if="scope.row.sex == 1">女</span>
        </template>
      </el-table-column>
      <el-table-column label="身份标识" align="center" prop="identity">
        <template slot-scope="scope">
          <dict-tag :options="identityOptions" :value="scope.row.identity"/>
        </template>
      </el-table-column>
      <el-table-column label="所在系" align="center" prop="faculty"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['campus:audit:user']"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <div style="padding: 0 10px;">
        <el-row style="padding: 12px;font-size: 15px;">
          <el-col :span="12">
            <span style="font-weight: bold">账号：</span>
            <span>{{detailData.username}}</span>
          </el-col>
          <el-col :span="12">
            <span style="font-weight: bold">昵称：</span>
            <span>{{detailData.nickName}}</span>
          </el-col>
        </el-row>
        <el-row style="padding: 12px;font-size: 15px;">
          <el-col :span="12">
            <span style="font-weight: bold">学/工号：</span>
            <span>{{detailData.number}}</span>
          </el-col>
          <el-col :span="12">
            <span style="font-weight: bold">姓名：</span>
            <span>{{detailData.name}}</span>
          </el-col>
        </el-row>
        <el-row style="padding: 12px;font-size: 15px;">
          <el-col :span="12">
            <span style="font-weight: bold">性别：</span>
            <span v-if="detailData.sex == 0">男</span>
            <span v-if="detailData.sex == 1">女</span>
          </el-col>
          <el-col :span="12">
            <span style="font-weight: bold">身份标识：</span>
            <span v-if="detailData.identity == 1">学生</span>
            <span v-if="detailData.identity == 2">老师</span>
            <span v-if="detailData.identity == 3">工作人员</span>
            <span v-if="detailData.identity == 4">管理员</span>
          </el-col>
        </el-row>
        <el-row style="padding: 12px;font-size: 15px;">
          <el-col :span="12">
            <span style="font-weight: bold">邮箱：</span>
            <span>{{detailData.email}}</span>
          </el-col>
          <el-col :span="12">
            <span style="font-weight: bold">所在系：</span>
            <span>{{detailData.faculty}}</span>
          </el-col>
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="passAudit">通 过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, getAuditUser, passAuditUser } from '@/api/campus/audit'

  export default {
    name: 'auditUser',
    data() {
      return {
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 审核的用户的相关信息表格数据
        auditUserList: [],
        // 身份表示 1学生 2老师 3工作人员 4管理员字典
        identityOptions: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          number: null
        },
        // 详细数据
        detailData: {}
      }
    },
    created() {
      this.getList()
      this.getDicts('user_identity').then(response => {
        this.identityOptions = response.data
      })
    },
    methods: {
      /** 查询所有要审核的用户的相关信息 */
      getList() {
        this.loading = true
        listUser(this.queryParams).then(response => {
          this.auditUserList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 数据重置
      reset() {
        this.detailData = {}
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 审核按钮操作 */
      handleAudit(row) {
        this.reset()
        const userId = row.userId
        getAuditUser(userId).then(response => {
          this.detailData = response.data
          this.open = true
          this.title = '审核用户信息'
        })
      },
      /** 审核通过按钮 */
      passAudit() {
        var self = this;
        self.$confirm('是否通过账号为"' + this.detailData.username + '"的审核', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return passAuditUser(self.detailData.userId)
        }).then(() => {
          self.getList();
          self.open = false;
          self.msgSuccess("审核成功")
        }).catch(() => {});
      }
    }
  }
</script>
